import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, DatePicker, Form, Modal, Row, Select} from 'antd';
import moment from 'moment'

const FormItem = Form.Item;
const R = require("ramda");
const dateFormat = 'YYYY-MM-DD';
const editLocalFeeDetail = Form.create()(class EditLocalFeeDetail extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,
            disabled: false
        }
    }

    handleOpen = (disabled) => {
        this.setState({ open: true, disabled });
    }

    handleCancel = () => {
        this.setState({ open: false });
        this.props.form.resetFields();
    }

    handleConfirm = () => {
        this.props.form.validateFields({ force: true }, (errors, values) => {
            if (errors) return;
            values.id = this.props.localFee.dataSource && this.props.localFee.dataSource.id ? this.props.localFee.dataSource.id : null;
            values.begindate = R.isNil(values.begindate) || R.isEmpty(values.begindate) ? "" : values.begindate.format(dateFormat);
            values.enddate = R.isNil(values.enddate) || R.isEmpty(values.enddate) ? "" : values.enddate.format(dateFormat);
            values.airlineId=parseInt (values.airlineId);
            values.supplierId=parseInt (values.supplierId);
            values.berthingId=parseInt (values.berthingId);
            // console.log(R.pick(Object.keys(values), this.props.localFee.dataSource));
            // console.log(values);
            if (!R.equals(R.pick(Object.keys(values), this.props.localFee.dataSource), values)) {
                this.props.dispatch({
                    type: R.isNil(values.id) ? 'localFee/addLocalFee' : 'localFee/updateLocalFee',
                    payload: {
                        ...values,
                        onSuccess: () => {
                            this.handleCancel();
                            if (this.props.refreshList) this.props.refreshList();
                        }
                    }
                })
            } else this.handleCancel();
        });
    }

    render() {
        let dataSource = this.props.localFee.dataSource ? this.props.localFee.dataSource : {};
        let supplierList = this.props.supplier.supplierList ? this.props.supplier.supplierList : [];
        let airlineList = this.props.airline.airlineList ? this.props.airline.airlineList : [];
        let berthingCompanyList = this.props.berthingCompany.berthingCompanyList ? this.props.berthingCompany.berthingCompanyList : [];
        let localFeeItemList = this.props.feeItem.localFeeItemList ? this.props.feeItem.localFeeItemList : [];
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 14 } };
        const { getFieldDecorator } = this.props.form;
        let footer = [
            <Button key="save" size="large" loading={this.props.loading} onClick={this.handleConfirm.bind(this)} style={{ display: this.state.disabled ? 'none' : 'inline-block' }}>保存</Button>,
            <Button key="cancel" size="large" loading={this.props.loading} onClick={this.handleCancel.bind(this)}>{this.state.disabled ? "关闭" : "取消"}</Button>
        ];
        return (
            <Modal maskClosable={false} visible={this.state.open} width={850} title={"本地费用（附加费）详情"} confirmLoading={this.props.loading} onCancel={this.handleCancel.bind(this)} footer={footer}>
                <Form>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="供应商代码" hasFeedback>
                                {getFieldDecorator('supplierId', {
                                    rules: [{
                                        required: true, message: '请选择供应商代码'
                                    }],
                                    initialValue: (R.isNil(dataSource.supplierId) || R.isEmpty(dataSource.supplierId)) ? undefined : dataSource.supplierId + ""
                                })(
                                    <Select style={{ width: '100%' }} disabled={this.state.disabled} showSearch placeholder="可通过输入关键字搜索" filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                        {
                                            !R.isNil(supplierList) ? supplierList.map(item => {
                                                return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.supplierCode}</Select.Option>);
                                            }) : ''
                                        }
                                    </Select>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="航线代码" hasFeedback>
                                {getFieldDecorator('airlineId', {
                                    rules: [{
                                        required: true, message: '请选择航线代码'
                                    }],
                                    initialValue: (R.isNil(dataSource.airlineId) || R.isEmpty(dataSource.airlineId)) ? undefined : dataSource.airlineId + ""
                                })(
                                    <Select style={{ width: '100%' }} disabled={this.state.disabled} showSearch placeholder="可通过输入关键字搜索" filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                        {
                                            !R.isNil(airlineList) ? airlineList.map(item => {
                                                return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.airlineCode}</Select.Option>);
                                            }) : ''
                                        }
                                    </Select>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="船公司代码" hasFeedback>
                                {getFieldDecorator('berthingId', {
                                    rules: [{
                                        required: true, message: '请选择船公司代码'
                                    }],
                                    initialValue: (R.isNil(dataSource.berthingId) || R.isEmpty(dataSource.berthingId)) ? undefined : dataSource.berthingId + ""
                                })(
                                    <Select style={{ width: '100%' }} disabled={this.state.disabled} showSearch placeholder="可通过输入关键字搜索" filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                        {
                                            !R.isNil(berthingCompanyList) ? berthingCompanyList.map(item => {
                                                return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.berthingCode}</Select.Option>);
                                            }) : ''
                                        }
                                    </Select>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="有效开始时间" hasFeedback>
                                {getFieldDecorator('begindate', {
                                    rules: [{
                                        required: true, message: '请选择有效开始时间'
                                    }],
                                    initialValue: (R.isNil(dataSource.begindate) || R.isEmpty(dataSource.begindate)) ? undefined : moment(dataSource.begindate, dateFormat)
                                })(
                                    <DatePicker disabled={this.state.disabled} style={{ width: '100%' }} showTime={{ format: 'YYYY-MM-DD' }}
                                        format="YYYY-MM-DD" placeholder="请选择有效开始时间" />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="有效截止时间" hasFeedback>
                                {getFieldDecorator('enddate', {
                                    rules: [{
                                        required: true, message: '请选择有效截止时间'
                                    }],
                                    initialValue: (R.isNil(dataSource.enddate) || R.isEmpty(dataSource.enddate)) ? undefined : moment(dataSource.enddate, dateFormat)
                                })(
                                    <DatePicker disabled={this.state.disabled} style={{ width: '100%' }} showTime={{ format: 'YYYY-MM-DD' }}
                                        format="YYYY-MM-DD" placeholder="请选择有效截止时间" />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </Modal >
        )
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.localFee, localFee: state.localFee, feeItem: state.feeItem, supplier: state.supplier,
        airline: state.airline, berthingCompany: state.berthingCompany
    };
}

export default connect(mapStateToProps)(editLocalFeeDetail);